#!/bin/bash
cat /dev/null > /etc/apt/sources.list
cat > /etc/apt/sources.list <<-EOF
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
##     or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl

# See https://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic main restricted
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates main restricted
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic universe
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic universe
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates universe
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic multiverse
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic multiverse
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates multiverse
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-backports main restricted universe multiverse
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb https://archive.canonical.com/ubuntu bionic partner
# deb-src https://archive.canonical.com/ubuntu bionic partner

deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security main restricted
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security main restricted
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security universe
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security universe
deb https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security multiverse
# deb-src https://mirror.tuna.tsinghua.edu.cn/ubuntu-ports bionic-security multiverse

EOF

apt update
apt upgrade -y
cat >> /etc/security/limits.conf <<-EOF
* soft nofile 2000000
* hard nofile 2000000
EOF

cat >> /etc/sysctl.conf <<-EOF
fs.file-max = 2097152
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_mtu_probing = 1
kernel.pid_max = 99999
kernel.printk = 4 4 1 7
kernel.panic = 10
kernel.sysrq = 0
kernel.shmmax = 4294967296
kernel.shmall = 4194304
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
vm.swappiness = 20
vm.dirty_ratio = 80
vm.dirty_background_ratio = 5
net.core.rmem_default = 31457280
net.core.wmem_default = 31457280
net.core.optmem_max = 25165824
net.ipv4.neigh.default.gc_thresh1 = 4096
net.ipv4.neigh.default.gc_thresh2 = 8192
net.ipv4.neigh.default.gc_thresh3 = 16384
net.ipv4.neigh.default.gc_interval = 5
net.ipv4.neigh.default.gc_stale_time = 120
net.netfilter.nf_conntrack_max = 10000000
net.netfilter.nf_conntrack_tcp_loose = 0
net.netfilter.nf_conntrack_tcp_timeout_established = 1800
net.netfilter.nf_conntrack_tcp_timeout_close = 10
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 10
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 20
net.netfilter.nf_conntrack_tcp_timeout_last_ack = 20
net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 20
net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 20
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 10
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.ip_no_pmtu_disc = 1
net.ipv4.route.flush = 1
net.ipv4.route.max_size = 8048576
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_congestion_control = htcp
net.ipv4.udp_mem = 65536 131072 262144
net.ipv4.udp_rmem_min = 16384
net.ipv4.udp_wmem_min = 16384
net.ipv4.tcp_max_orphans = 400000
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_ecn = 2
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.rp_filter = 1
EOF

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
lsmod | grep bbr

apt install -y locales
export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
locale-gen zh_CN.UTF-8

cat >> /etc/profile <<-EOF
export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
TZ='Asia/Shanghai'; export TZ

EOF

cd /var/
dd if=/dev/zero of=swapfile bs=1M count=2048
/sbin/mkswap swapfile
/sbin/swapon swapfile
/sbin/swapon -s
chmod 0644 /var/swapfile
echo "/var/swapfile swap swap defaults 0 0" >>/etc/fstab

reboot